
// ************************************
// * GStore - Inventory System - thomasfn *
// ************************************
// SERVERSIDE

local mPly = _R.Player

function mPly:InitInv()
	local inv = gstore.CreateItem( "inventory" )
	inv.Owner = self
	inv:FromWorld()
	self:SetNWInt( "gstore_inv", inv:Index() )
	self.Inventory = inv
	return inv
end

function mPly:GiveItem( item )
	self.Inventory:AddItem( item )
end

function mPly:DropItem( item )
	self.Inventory:RemoveItem( item )
end

function mPly:CanTakeItem( item )
	return self.Inventory:CanHoldItem( item )
end

function mPly:CanTakeItem_Reason( item )
	return self.Inventory:CanHoldItem_Reason( item )
end

function mPly:HasItem( item )
	return self.Inventory:HasItem( item )
end

function mPly:RefreshInv( delay )
	timer.Simple( delay or 0, function()
		self.Inventory:Sync( self )
	end )
end

local function PlayerInitialSpawn( ply )
	local inv = ply:InitInv()
	// Here we load their inventory from file
end
hook.Add( "PlayerInitialSpawn", "gstore.initspawn", PlayerInitialSpawn )

local function cmdDbgGiveItem( ply, com, args )
	local class = args[1]
	if (!class) then return ply:ChatPrint( "Invalid item!" ) end
	local item = gstore.CreateItem( class, ply:GetShootPos() )
	if (!item) then return ply:ChatPrint( "Invalid item!" ) end
	if (!ply:CanTakeItem( item )) then return end
	ply:GiveItem( item )
	ply:ChatPrint( "Received " .. class .. "!" )
end
concommand.Add( "gstore_debug_give", cmdDbgGiveItem )

local function cmdDbgSpawnItem( ply, com, args )
	local class = args[1]
	if (!class) then return ply:ChatPrint( "Invalid item!" ) end
	local pos = ply:GetEyeTrace().HitPos + Vector( 0, 0, 32 )
	local item = gstore.CreateItem( class, pos )
	if (!item) then return ply:ChatPrint( "Invalid item!" ) end
	ply:ChatPrint( "Spawned " .. class .. "!" )
end
concommand.Add( "gstore_debug_spawn", cmdDbgSpawnItem )